package com.neusoft.yuejian_01.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian_01.R;
import com.neusoft.yuejian_01.base.BaseActivity;
import com.neusoft.yuejian_01.javabean.AttentionBean;
import com.neusoft.yuejian_01.javabean.IsAttentionBean;
import com.neusoft.yuejian_01.javabean.IsTrainReg;
import com.neusoft.yuejian_01.javabean.NoRegBean;
import com.neusoft.yuejian_01.javabean.TrainBean;
import com.neusoft.yuejian_01.listener.RetrofitListener;
import com.neusoft.yuejian_01.model.AttentionModel;
import com.neusoft.yuejian_01.model.Const;
import com.neusoft.yuejian_01.model.ConstPhoto;
import com.neusoft.yuejian_01.model.TrainModel;

import it.sephiroth.android.library.picasso.Picasso;

public class TrainDetailActivity extends BaseActivity implements RetrofitListener {
    private TextView titleView;
    private TextView trainphoneView;
    private TextView traintimeView;
    private TextView trainpriceView;
    private TextView traintypeView;
    private TextView trainaddressView;
    private TextView trainnumberView;
    private TextView clubnameView;
    private TextView traininfoView;
    protected Context context;
    private Button regButton;//报名按钮
    private ImageButton button_back,trainButton,callButton;//返回按钮、关注按钮、拨打电话按钮
    public ImageView trainImageView;
    private int t_id;
    private boolean flags;
    private boolean flagr;

     protected void initView() {
         setContentView(R.layout.activity_train_detail);
        titleView = (TextView) findViewById(R.id.title_train);
        traintimeView = (TextView) findViewById(R.id.train_time);
        trainpriceView = (TextView) findViewById(R.id.train_price);
        traintypeView = (TextView) findViewById(R.id.train_type);
        trainaddressView = (TextView) findViewById(R.id.train_address);
        trainnumberView = (TextView) findViewById(R.id.train_number);
        clubnameView = (TextView) findViewById(R.id.club_train);
        trainphoneView = (TextView) findViewById(R.id.train_phone);
        traininfoView = (TextView) findViewById(R.id.train_info);
        trainImageView =(ImageView) findViewById(R.id.train_image);
        button_back = (ImageButton) findViewById(R.id.button_back);
        callButton = (ImageButton) findViewById(R.id.call_Button);
        trainButton = (ImageButton) findViewById(R.id.trainButton);
        regButton = (Button) findViewById(R.id.activityreg_Button);
    }
     protected void initEvents() {
        trainButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AttentionModel attentionModel = new AttentionModel();
                attentionModel.userCollectTrain(String.valueOf(getUser_id()),String.valueOf(t_id),TrainDetailActivity.this);
            }
        });
        regButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TrainModel trainModel1 = new TrainModel();
                trainModel1.getIsNotReg(String.valueOf(getUser_id()),String.valueOf(t_id),TrainDetailActivity.this);
            }
        });
        callButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + trainphoneView.getText().toString()));
                if (ActivityCompat.checkSelfPermission(TrainDetailActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions// here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent);
            }
        });
        button_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
     protected void initData() {
        t_id = getIntent().getIntExtra("t_id", -1);
        TrainModel trainModel = new TrainModel();
        trainModel.traindetail(String.valueOf(t_id), this);
        AttentionModel attentionModel = new AttentionModel();
        attentionModel.isCollected(String.valueOf(getUser_id()), String.valueOf(t_id), "0", this);//flag=0.判断培训关注状态
        TrainModel trainModels = new TrainModel();
        trainModels.getIsReg(String.valueOf(getUser_id()), String.valueOf(t_id), this);
    }
    @Override
    public void onSuccess(Object object, int flag) {
        switch (flag)
        {
            case Const.TrainDeatl:
                //flag标识为1，获取培训详情
                TrainBean traindetail =  (TrainBean)object;
                titleView.setText(traindetail.getT_name());
                traintimeView.setText(traindetail.getT_time());
                trainpriceView.setText(String.valueOf(traindetail.getT_price()));
                if (String.valueOf(traindetail.getT_type()).equals("0")){
                    traintypeView.setText("入门");
                }
                if (String.valueOf(traindetail.getT_type()).equals("1")) {
                    traintypeView.setText("进阶");
                }
                if (String.valueOf(traindetail.getT_type()).equals("2")) {
                    traintypeView.setText("高级");
                }
                trainaddressView.setText(traindetail.getT_address());
                trainnumberView.setText(String.valueOf(traindetail.getT_number()));
                clubnameView.setText(traindetail.getC_name());
                trainphoneView.setText(traindetail.getT_phone());
                traininfoView.setText("\u3000\u3000"+traindetail.getT_info());
                final String trainPic = ConstPhoto.BASE_URL+ConstPhoto.TrainFile+traindetail.getT_picture();
                Picasso.with(context)
                        .load(trainPic)
                        .placeholder(R.drawable.pic2)
                        .into(trainImageView);
                break;
            case Const.IsAttention:
                //flag标识为2，判断关注状态
                IsAttentionBean isAttentionBean=(IsAttentionBean)object;
                if (isAttentionBean.getCollected().equals("1")){
                    //已关注
                    flags=true;
                    trainButton.setImageResource(R.drawable.ic_attention_checked);
                }
                else {
                    //未关注
                    flags=false;
                    trainButton.setImageResource(R.drawable.ic_attention);
                }
                break;
            case Const.IsTrainReg:
                //flag标识为3，判断报名状态
                IsTrainReg isTrainReg=(IsTrainReg)object;
                if (isTrainReg.getTn_paystate().equals("1")){
                    //去支付
                    flagr=true;
                    regButton.setText("取消报名");
                }
                if (isTrainReg.getTn_paystate().equals("0")){
                    //已支付
                    regButton.setText("查看订单");
                    regButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent();
                            intent.setClass(TrainDetailActivity.this,TrainOrderActivity.class);
                            intent.putExtra("t_id",t_id);
                            startActivityForResult(intent,1);
                        }
                    });
                }
                if (isTrainReg.getTn_paystate().equals("2")) {
                    //未报名
                    flagr=false;
                    regButton.setText("立即报名");
                    regButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent();
                            intent.setClass(TrainDetailActivity.this,TrainRegActivity.class);
                            intent.putExtra("t_id",t_id);
                            startActivityForResult(intent,1);
                        }
                    });
                }
                break;
            case Const.TrainAttention:
                AttentionBean attentionBean=(AttentionBean) object;
                if (attentionBean.getSuccess().equals("1")){
                    Log.e(attentionBean.getSuccess(),"---------");
                    if(flags){
                        flags=false;
                        trainButton.setImageResource(R.drawable.ic_attention);
                    }else {
                        flags=true;
                        trainButton.setImageResource(R.drawable.ic_attention_checked);
                    }
                    Toast.makeText(TrainDetailActivity.this, "OK", Toast.LENGTH_SHORT).show();
                }
                break;
            case Const.NoReg:
                NoRegBean noRegBean=(NoRegBean) object;
                if (noRegBean.getSuccess().equals("1")){
                    Log.e(noRegBean.getSuccess(),"---------");
                    if(flagr) {
                        flagr = true;
                        regButton.setText("立即报名");
                        regButton.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent=new Intent();
                                intent.setClass(TrainDetailActivity.this,TrainRegActivity.class);
                                intent.putExtra("t_id",t_id);
                                startActivityForResult(intent,1);
                            }
                        });
                    }
                    Toast.makeText(TrainDetailActivity.this,"取消报名成功",Toast.LENGTH_SHORT).show();
                }
                break;

        }
    }
    @Override
    public void onFail() {
        Toast.makeText(this,"fail",Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();//初始化
        initEvents();//点击事件处理
        initData();//

    }
}
